<template>
    <div>
        <Card>
            <!-- search start -->
            <div class="search">
                <Form :model="search" class="search_box" inline label-position="right">
                    <FormItem label="分部 -> 楼层：">
                        <Cascader class="cascader" :value="cascadeArr" :data="cascadeList" change-on-select @on-change="cascadeSelection" :clearable="false"></Cascader>
                    </FormItem>

                    <FormItem>
                        <Button icon="md-search" type="primary" @click="searchList">搜索</Button>
                    </FormItem>
                </Form>

            </div> <!-- end search -->
            <Table ref="table" :loading="loading" stripe :columns="columns1" :data="data1"></Table>
        </Card>

    </div>
</template>

<script>
    import axios from '@/libs/api.request'
    import {
        mapGetters
    } from 'vuex'
    export default {
        name: 'floor-info',
        data() {
            return {
                loading: false,

                // 搜索对象
                search: {
                    branchid: -1,
                    floorid: -1
                },

                cascadeArr: [-1],

                columns1: [{
                        type: 'index',
                        width: 60,
                        align: 'center',
                        className: 'workHeader'
                    },
                    {
                        title: '分部楼层 ',
                        width: 120,
                        align: 'center',
                        key: 'floor_zw',
                        className: 'workHeader'
                    },
                    {
                        title: '工位总数',
                        align: 'center',
                        className: 'workStation',
                        render: (h, params) => {
                            return h('span', params.row.gw_info.total_num)
                        },
                    },
                    {
                        title: '工位空置',
                        align: 'center',
                        className: 'workStation',
                        render: (h, params) => {
                            return h('span', params.row.gw_info.vacant_ws_num)
                        },
                    },
                    {
                        title: '工位已使用',
                        align: 'center',
                        className: 'workStation',
                        render: (h, params) => {
                            return h('span', params.row.gw_info.use_ws_num)
                        },
                    },
                    {
                        title: '工位出租率',
                        align: 'center',
                        className: 'workStation',
                        render: (h, params) => {
                            return h('span', params.row.gw_info.use_rate + '%')
                        },
                    },
                    {
                        title: '工位办公人数',
                        width: 80,
                        align: 'center',
                        className: 'workStation',
                        render: (h, params) => {
                            return h('span', params.row.gw_info.person_num)
                        },
                    },
                    {
                        title: '办公室总数',
                        align: 'center',
                        className: 'workRoom',
                        render: (h, params) => {
                            return h('span', params.row.bgs_info.total_num)
                        },
                    },
                    {
                        title: '办公室空置',
                        align: 'center',
                        className: 'workRoom',
                        render: (h, params) => {
                            return h('span', params.row.bgs_info.vacant_ws_num)
                        },
                    },
                    {
                        title: '办公室已使用',
                        width: 80,
                        align: 'center',
                        className: 'workRoom',
                        render: (h, params) => {
                            return h('span', params.row.bgs_info.use_ws_num)
                        }
                    },
                    {
                        title: '办公室出租率',
                        width: 80,
                        align: 'center',
                        className: 'workRoom',
                        render: (h, params) => {
                            return h('span', params.row.bgs_info.use_rate + '%')
                        },
                    },
                    {
                        title: '办公室办公人数',
                        width: 90,
                        align: 'center',
                        className: 'workRoom',
                        render: (h, params) => {
                            return h('span', params.row.bgs_info.person_num)
                        },
                    },
                    {
                        title: '工位+办公室总数',
                        width: 110,
                        align: 'center',
                        className: 'workTotal',
                        render: (h, params) => {
                            return h('span', params.row.total_info.total_num)
                        },
                    },
                    {
                        title: '工位+办公室空置总数',
                        width: 110,
                        align: 'center',
                        className: 'workTotal',
                        render: (h, params) => {
                            return h('span', params.row.total_info.vacant_ws_num)
                        },
                    },
                    {
                        title: '工位+办公室已使用',
                        width: 110,
                        align: 'center',
                        className: 'workTotal',
                        render: (h, params) => {
                            return h('span', params.row.total_info.use_ws_num)
                        }
                    },
                    {
                        title: '工位+办公室出租率',
                        width: 110,
                        align: 'center',
                        className: 'workTotal',
                        render: (h, params) => {
                            return h('span', params.row.total_info.use_rate + '%')
                        },
                    },
                    {
                        title: '工位+办公室办公人数',
                        width: 110,
                        align: 'center',
                        className: 'workTotal',
                        render: (h, params) => {
                            return h('span', params.row.total_info.person_num)
                        },
                    },
                ],
                data1: []
            }
        },
        methods: {
            ...mapGetters(['getBranchList']),

            // 级联选择
            cascadeSelection(value) {
                if (value.length == 1) {
                    this.search.branchid = value[0]
                    this.search.floorid = -1
                    return false
                }
                this.search.branchid = value[0]
                this.search.floorid = value[1]
            },

            // 获取楼层信息
            getFloorInfo() {
                this.loading = true
                let data = this.search
                axios.ajax_post('admin-space-ajax_get_floor_total', data).then(res => {
                    this.loading = false
                    if (res.data.code === 1) {
                        this.data1 = res.data.data.list
                        this.handleData(this.data1)
                    } else {
                        this.$Notice.error({
                            title: res.data.msg,
                            duration: 3
                        })
                    }
                })

            },

            // 搜索
            searchList() {
                this.getFloorInfo()
            },

            // 计算合计
            handleData(data) {

                const sums = {
                    bgs_info: {
                        person_num: 0,
                        total_num: 0,
                        use_rate: 0,
                        use_ws_num: 0,
                        vacant_ws_num: 0,
                    },
                    gw_info: {
                        person_num: 0,
                        total_num: 0,
                        use_rate: 0,
                        use_ws_num: 0,
                        vacant_ws_num: 0,
                    },
                    total_info: {
                        person_num: 0,
                        total_num: 0,
                        use_rate: 0,
                        use_ws_num: 0,
                        vacant_ws_num: 0,
                    },
                    floor_zw: '合计'
                }
                data.forEach(item => {

                    // 工位合计
                    sums.gw_info.total_num += item.gw_info.total_num
                    sums.gw_info.vacant_ws_num += item.gw_info.vacant_ws_num
                    sums.gw_info.use_ws_num += item.gw_info.use_ws_num
                    sums.gw_info.person_num += item.gw_info.person_num
                    // 办公室合计
                    sums.bgs_info.total_num += item.bgs_info.total_num
                    sums.bgs_info.vacant_ws_num += item.bgs_info.vacant_ws_num
                    sums.bgs_info.use_ws_num += item.bgs_info.use_ws_num
                    sums.bgs_info.person_num += item.bgs_info.person_num
                    // 办公室+工位合计
                    sums.total_info.total_num += item.total_info.total_num
                    sums.total_info.vacant_ws_num += item.total_info.vacant_ws_num
                    sums.total_info.use_ws_num += item.total_info.use_ws_num
                    sums.total_info.person_num += item.total_info.person_num
                })
                sums.gw_info.use_rate = sums.gw_info.total_num > 0 ? Math.round(sums.gw_info.use_ws_num / sums.gw_info.total_num * 100) : 0
                sums.bgs_info.use_rate = sums.bgs_info.total_num > 0 ? Math.round(sums.bgs_info.use_ws_num / sums.bgs_info.total_num * 100) : 0
                sums.total_info.use_rate = sums.total_info.total_num > 0 ? Math.round(sums.total_info.use_ws_num / sums.total_info.total_num * 100) : 0
                this.data1.push(sums)
            }
        },
        computed: {
            cascadeList() {
                let list = JSON.parse(JSON.stringify(this.getBranchList()))
                list.forEach((item) => {
                    item.children.forEach((floor) => {
                        delete floor.children
                    })
                })
                list.unshift({
                    value: -1,
                    label: '全部'
                })
                return list
            },
        },
        created() {
            this.getFloorInfo()
        }
    }
</script>

<style lang='less' scoped>
    .search {
        .search_box {
            display: flex;

            .option {
                width: 100px;
            }

            .ivu-form-item {
                margin-right: 12px;
                display: flex;
                justify-content: flex-start;
                margin-bottom: 20px;
            }
        }
    }
</style>